package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.dto.ImmunizationRecordDto;
import com.jiantong.work.impl.dao.ImmunizationRecordDao;
import com.jiantong.work.impl.manager.ImmunizationRecordManager;
import com.jiantong.work.model.ImmunizationRecord;
import com.jiantong.work.service.ImmunizationRecordWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by Administrator on 2017/3/30.
 */
@Slf4j
@Service
@RpcProvider
public class ImmunizationRecordWriteServiceImpl implements ImmunizationRecordWriteService {

    private ImmunizationRecordManager immunizationRecordManager;
private ImmunizationRecordDao immunizationRecordDao;
    @Autowired
    public ImmunizationRecordWriteServiceImpl(ImmunizationRecordManager immunizationRecordManager,ImmunizationRecordDao immunizationRecordDao) {
        this.immunizationRecordManager = immunizationRecordManager;
        this.immunizationRecordDao=immunizationRecordDao;
    }

    @Override
    public Response<Map<String,Object>> create(ImmunizationRecordDto immunizationRecordDto, Integer toStatus) {
        try {
            return Response.ok(immunizationRecordManager.createImmunizationRecord(immunizationRecordDto,toStatus));
        } catch (Exception e) {
            log.error("failed.to.create {},case:{}" ,immunizationRecordDto,Throwables.getStackTraceAsString(e));
            return Response.fail("create.immunizationRecord");
        }
    }

    @Override
    public Response<Map<String,Object>> update(ImmunizationRecordDto immunizationRecordDto,Integer toStatus) {
        try {
            return Response.ok(immunizationRecordManager.updateImmunizationRecord(immunizationRecordDto,toStatus));
        }catch (Exception e){
            log.error("failed.to.update {},case:{}" ,immunizationRecordDto,Throwables.getStackTraceAsString(e));
            return Response.fail("immunization.record..update.fail");
        }
    }

    @Override
    public Boolean update(ImmunizationRecord immunizationRecord) {
       return immunizationRecordDao.update(immunizationRecord);
    }
}
